
//init state
const state = () => ({
  param:[
/*    {serviceId: 1,serviceContent: "屏碎保一年 ￥249"},
    {serviceId: 2,serviceContent: "屏碎保+延长保 ￥250"},
    {serviceId: 3,serviceContent: "0享无忧 ￥258"},
    {serviceId: 4,serviceContent: "OPPO Care+ ￥469"},
    {serviceId: 5,serviceContent: "延长保半年 ￥79"},
    {serviceId: 6,serviceContent: "延长保一年 ￥129"}*/
  ]
})

//getters
const getters = {
  getSerNameBySerId(state){
    return function (serId) {//通过保修服务编号查询保修服务名称
      if (!serId) return undefined;
      return  state.param.filter(item => {
        return item.serviceId == serId;
      })[0].serviceContent;
    }
  },
  getSerIdBySerName(state){//通过服务名字查找服务编号
    return function (serName) {
      if (!serName || serName == "") return undefined;
      return  state.param.filter(item => {
        return item.serviceContent == serName;
      })[0].serviceId;
    }
  },
  refreshServiceMsgSessionStorageData(state){//刷新session中的方法
    sessionStorage.setItem("ServiceMsg",JSON.stringify(state.param))
  },
  getSerPriceBySerId(state){//通过保修服务编号获取保修服务价格
    return function (serId) {
      if (!serId || serId == "") return undefined;
      return state.param.filter(item => {
        return item.serviceId == serId;
      })[0].serviceContent.split("￥")[1]
    }
  }
}

//actions
const actions = {
  loadDataForServiceMsg({commit},payload){//将request的数据保存到store当中
    commit("loadDataForServiceMsgMut",payload)
  },
  addDataForServiceMsgBySessionStorage({commit}){//将session中的数据添加到store当中
    commit("addDataForServiceMsgBySessionStorageMut")
  }
}

//mutations
const mutations = {
  loadDataForServiceMsgMut(state,val){//将request的数据保存到store当中
    state.param = [...val];
  },
  addDataForServiceMsgBySessionStorageMut(state){//将session中的数据添加到store当中
    state.param = [...JSON.parse(sessionStorage.getItem("ServiceMsg"))];
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
